﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using HgCo.WindowsLive.SkyDrive;
using API.WindowsLive.SkyDrive;
using System.IO;

namespace API.WindowsLive.SkyDrive.NUnit
{
    [TestFixture]
    public class NUnit
    {
        private SkyDriveWebClient skyDrive;

        [TestFixtureSetUp]
        public void Login()
        {

            Dictionary<String, String> dico = SkyDriveSynchronizerStarter.GetUserNameAndPassword();
            skyDrive = new SkyDriveWebClient();

            try
            {
                skyDrive.LogOn(dico["username"], dico["password"]);
            }
            catch (Exception ex)
            {
                Assert.Fail("Could not log in " + ex.Message);
            }
        }

        [Test]
        public void ListFolders()
        {
            WebFolderInfo[] webFolders = skyDrive.ListRootWebFolders();
            foreach (WebFolderInfo webFolder in webFolders)
            {
                if (webFolder.Name.Equals("TestFolder"))
                {
                    Assert.Pass();
                    return;
                }
            }

            Assert.Fail();
        }

        [Test]
        public void ListIncorrectFolders()
        {
            WebFolderInfo[] webFolders = skyDrive.ListRootWebFolders();
            foreach (WebFolderInfo webFolder in webFolders)
            {
                if (webFolder.Name.Equals("TestFolderMissing"))
                    Assert.Fail();
            }

            Assert.Pass();
        }

        [Test]
        public void ListSubWebFolderFiles()
        {
            WebFolderInfo[] webFolders = skyDrive.ListRootWebFolders();
            foreach (WebFolderInfo webFolder in webFolders)
            {
                if (webFolder.Name.Equals("TestFolder"))
                {
                    WebFolderInfo retrievedWebFolder = skyDrive.GetWebFolder(webFolder);
                    WebFileInfo[] webFiles = skyDrive.ListSubWebFolderFiles(retrievedWebFolder);

                    foreach (WebFileInfo webFile in webFiles)
                    {
                        if (webFile.FullName.Equals("TestFile.txt"))
                        {
                            Assert.Pass();
                            return;
                        }
                    }
                    Assert.Fail();
                }
            }
        }

        [Test]
        public void ListIncorrectSubWebFolderFiles()
        {
            WebFolderInfo[] webFolders = skyDrive.ListRootWebFolders();
            foreach (WebFolderInfo webFolder in webFolders)
            {
                if (webFolder.Name.Equals("TestFolder"))
                {
                    WebFolderInfo retrievedWebFolder = skyDrive.GetWebFolder(webFolder);
                    WebFileInfo[] webFiles = skyDrive.ListSubWebFolderFiles(retrievedWebFolder);

                    foreach (WebFileInfo webFile in webFiles)
                    {
                        if (webFile.FullName.Equals("TestFileMissing.txt"))
                        {
                            Assert.Fail();
                            return;
                        }
                    }

                    Assert.Pass();
                }
            }

        }

        //[Test]
        //public void DownloadFile()
        //{
        //    WebFolderInfo[] webFolders = skyDrive.ListRootWebFolders();
        //    foreach (WebFolderInfo webFolder in webFolders)
        //    {
        //        if (webFolder.Name.Equals("TestFolder"))
        //        {
        //            WebFolderInfo retrievedWebFolder = skyDrive.GetWebFolder(webFolder);
        //            WebFileInfo[] webFiles = skyDrive.ListSubWebFolderFiles(retrievedWebFolder);

        //            foreach (WebFileInfo webFile in webFiles)
        //            {
        //                if (webFile.FullName.Equals("TestFile.txt"))
        //                {
        //                    try
        //                    {
        //                        String returnedText = String.Empty;

        //                        using (Stream stream = skyDrive.DownloadWebFile(webFile))
        //                        {
        //                            using (FileStream fileStream = new FileStream("C:\\" + webFile.FullName, FileMode.OpenOrCreate))
        //                            {
        //                                byte[] buffer = new byte[64 * 1024];
        //                                int count = 0;
        //                                //long downloadSoFar = 0;
        //                                //long startTicks = DateTime.Now.Ticks;

        //                                while ((count = stream.Read(buffer, 0, buffer.Length)) > 0)
        //                                {
        //                                    fileStream.Write(buffer, 0, count);
        //                                    //downloadSoFar += (long)count;

        //                                    //if ((DateTime.Now.Ticks - startTicks) > waitTicksBeforeRaiseProgressEvent)
        //                                    //{
        //                                    //    FileChanged(this, new FileChangedEventArgs(webFile.PathUrl, webFile.FullName, downloadSoFar, (long)webFile.SizeMax, FileChangedEventArgs.EventTypeEnum.downloadProgress));
        //                                    //    startTicks = DateTime.Now.Ticks;
        //                                    //}
        //                                }

        //                                if (File.Exists("C:\\" + webFile.FullName))
        //                                    returnedText = File.ReadAllText("C:\\" + webFile.FullName);
                                        
        //                                Assert.Fail("Third here");
        //                                return;
        //                            }
        //                        }
        //                        Assert.True(returnedText.Equals("What ever traver"));
        //                        return;
        //                    }
        //                    catch (Exception ex)
        //                    {
        //                        Assert.Fail(ex.Message);
        //                    }
        //                }
        //            }
        //        }
        //    }

        //    Assert.Fail();
        //}
    }


}
